package Controller;

import Service.OpenService;
import Utils.JsonResult;
import Utils.Open;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Map;

@Controller
public class OpenController
{
    @Autowired
    private OpenService openService;

    @ResponseBody
    @RequestMapping(value = "/API/Open/Open",method = RequestMethod.POST)
    public JsonResult CreateOpen(@RequestBody String params, HttpSession session)
    {
        JsonResult jsonResult = new JsonResult();

        try {
            ObjectMapper objectMapper = new ObjectMapper();
            Map<String,String> params_map = objectMapper.readValue(params,Map.class);
            String UserId = null;
            UserId = (String) session.getAttribute("UserId");
            String Account = null;
            Account = params_map.get("Account");
            String Idcard = null;
            Idcard  = params_map.get("Idcard");
            //根据UserId查找开户信息
            Open open = openService.GetOpenById(UserId);
            if(open != null)
            {
                jsonResult.setErrorCode("开户信息已上报，请不要重复上报");
                return jsonResult;
            }
            if(UserId == null || Account == null || Idcard == null)
            {
                jsonResult.setErrorCode("银行卡号或身份证号不能为空");
            }
            else
            {
                String open_time = String.valueOf(System.currentTimeMillis());
                Boolean status = openService.AddOpen(UserId,Idcard,Account,open_time);
                if(status)
                {
                    jsonResult.setErrorCode("开户信息已上报，等待管理员审核");
                }
                else
                {
                    jsonResult.setErrorCode("发生错误，请联系管理员");
                }
            }
        }
        catch (Exception e)
        {
            jsonResult.setErrorCode("发生错误，请联系管理员");
        }
        finally {
            return jsonResult;
        }
    }
    @ResponseBody
    @RequestMapping(value = "/API/Open/Open",method = RequestMethod.GET)
    public JsonResult GetOpen(HttpSession session)
    {
        JsonResult jsonResult = new JsonResult();
        try{
            String userid = (String) session.getAttribute("UserId");
            ArrayList<Open> openArrayList = new ArrayList<>();
            openArrayList.add(openService.GetOpenById(userid));
            jsonResult.setErrorCode("调用成功");
            jsonResult.setData(openArrayList);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            jsonResult.setErrorCode("发生异常，请联系管理员");
        }
        finally {
            return jsonResult;
        }
    }
    @ResponseBody
    @RequestMapping(value = "/API/Open/PassOpen",method = RequestMethod.POST)
    public JsonResult PassOpen(@RequestBody String params)
    {
        JsonResult jsonResult = new JsonResult();
        try{
            ObjectMapper objectMapper = new ObjectMapper();
            Map<String,String> params_map = objectMapper.readValue(params,Map.class);
            String UserId = params_map.get("UserId");
            boolean status = openService.PassOpen(UserId);
            if(status)
            {
                jsonResult.setErrorCode("调用成功");
            }
            else {
                jsonResult.setErrorCode("调用失败");
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            jsonResult.setErrorCode("发生异常，请联系管理员");
        }
        finally {
            return jsonResult;
        }
    }

}
